package cn.itcast.controller;

import cn.itcast.domain.Admin;
import cn.itcast.domain.Client;
import cn.itcast.domain.Room;
import cn.itcast.mapper.ClientMapper;
import cn.itcast.mapper.RoomMapper;
import cn.itcast.service.ClientService;
import cn.itcast.service.RoomService;
import cn.itcast.service.impl.RoomServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;
import java.util.zip.DataFormatException;

@SuppressWarnings("all")
@Controller
@RequestMapping("client")
public class ClientController {
    @Autowired
    private ClientService clientService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private ClientMapper mapper;
    @Autowired
    private RoomMapper roomMapper;

    @RequestMapping("findClient")
    @ResponseBody
    public Client findClient(Integer integer){

        return clientService.findClient(integer);
    }

    /**
     * 查询住的客户
     * @param modelAndView
     * @return
     */
    @RequestMapping("findAll")
    public ModelAndView findAll( ModelAndView modelAndView,String currentPage,String rows) {
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        if(rows==null||"".equals(rows)){
            rows="5";
        }
        List<Client> list = clientService.findNow(currentPage,rows);
        PageInfo pageInfo=new PageInfo(list);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.addObject("clientList",list);
        modelAndView.setViewName("client-list");
        return modelAndView;
    }

    /**
     * 查询历史住的客户
     * @param modelAndView
     * @return
     */
    @RequestMapping("findHis")
    public ModelAndView findHis( ModelAndView modelAndView,String currentPage,String rows) {
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        if(rows==null||"".equals(rows)){
            rows="5";
        }
        List<Client> list = clientService.findHis(currentPage,rows);
        PageInfo pageInfo=new PageInfo(list);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.addObject("clientList",list);
        modelAndView.setViewName("history-client");
        return modelAndView;
    }

    /**
     * 点击房间图片
     * 添加客户
     * @param client
     * @return
     */
    @RequestMapping("/add")
    public String add(Client client,HttpServletRequest request){
        System.out.println(client);
        String rid = request.getParameter("rid");
        System.out.println(rid);
        int rid1=0;
        if(rid!=null&&!"".equals(rid)){
            rid1 = Integer.parseInt(rid);
        }
        client.setRoomNum(rid1);
        roomMapper.updateRoomFlag(rid1);
        clientService.add(client);
        return "redirect:http://localhost/room/findAll";
    }

    @RequestMapping("/clientDate")
    public Date clientDate(int roomNumber){
        Date clientDate = clientService.findClientDate(roomNumber);
        return clientDate;

    }

    @RequestMapping("/findByRoomNum")
    @ResponseBody
    public Client findByRoomNum(String rid){
        System.out.println(rid);
        Client client = mapper.findByRoomNum(rid);
        return client;
    }

    /**
     * 退房
     */
    @RequestMapping("/del")
    public String del(HttpServletRequest request){
        String cid = request.getParameter("cid");
        String rid = request.getParameter("rid");
        int cid1 = Integer.parseInt(cid);
        clientService.updateFlagById(cid1);
        int rid1 = Integer.parseInt(rid);
        roomMapper.updateFlagByRid(rid1);
        return "redirect:http://localhost/room/findAll";
    }
}
